package com.example.demo.controller;

import com.example.demo.entity.Stu2;
import jakarta.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class VaildController {
    @RequestMapping("/valid_input")
    public String input(Stu2 stu2) {
        return "/valid_input";
    }
    @RequestMapping("/valid_check")
    public String check(@Valid Stu2 stu2, BindingResult bindingResult) {//校验的结果要紧跟着要校验的对象
        if(bindingResult.hasErrors()) {
            return "/valid_input";//重新输入
        }
        return "/valid_res";//显示结果
    }
}


//import com.example.demo.entity.Stu2;
//import jakarta.validation.Valid;
//import org.springframework.stereotype.Controller;
//import org.springframework.validation.BindingResult;
//import org.springframework.web.bind.annotation.RequestMapping;
////@RequestMapping 注解的主要作用是将一个 HTTP 请求路径和一个处理方法或类进行关联，
//// 当客户端发送符合该路径的请求时，Spring 框架会将请求转发到对应的处理方法进行处理。
//@Controller
//public class ValidController {
//    @RequestMapping("/valid_input")
//    public String input(Stu2 stu2){
//        return "/valid_input";
//    }
//    @RequestMapping("/valid_check")
//    public String check(@Valid Stu2 stu2, BindingResult bindingResult){
//        if(bindingResult.hasErrors()){
//            return "/valid_input";
//        }
//        //repository.save(stu2);
//        return "/valid_res";
//    }
//}
